﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Agnes.Libaray;
using Agnes.Core;
using Caliburn.Micro;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 浏览文章命令
    /// </summary>
    public class PreviewArticleCmd
    {
        private static PreviewArticleCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static PreviewArticleCmd Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PreviewArticleCmd();
                }
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        PreviewArticleCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("Article Preview Cmd", typeof(PreviewArticleCmd));
        }

        /// <summary>
        /// 浏览文章的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 浏览文章执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                Article selNode = MyDocsLibManager.Instance.ArticleManager.SelectedNode as Article;
                if (selNode == null)
                {
                    return;
                }

                IDocumentPanel documentPanel = DocumentPanelsHelper.GetDocumentByModel(selNode);
                selNode.PreviewCount += 1;
                if (documentPanel != null)
                {
                    AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel = documentPanel;
                    ((documentPanel.Content as ArticleView).DataContext as ArticleViewModel).IsEditMode = false;
                }
                else
                {
                    ArticleViewModel vm = new ArticleViewModel(selNode);
                    AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.Add(vm);
                }
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("MyDocsLib--预览文章失败，错误：" + ex.Message);
            }
        }

        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (AgnesServiceManager.AgnesSlnService.CurrentSln == null 
                || (MyDocsLibManager.Instance.ArticleManager.SelectedNode is Article) == false)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
